//
//  YDHomeTableViewHeaderView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/8.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDHomeTableViewHeaderView.h"


//#define headerViewH 44.0
@interface YDHomeTableViewHeaderView()

@property (nonatomic , assign) CGFloat headerViewH;
//指示器
@property (nonatomic , weak) UIView *lineView;
//盛放按钮的数组
@property (nonatomic , strong) NSMutableArray *buttonArray;
//记录上一次选中的按钮
@property (nonatomic , weak) SYNoHeightedButton *previousButton;


//分割线
@property (nonatomic , weak) UIView *LineView;



@end


@implementation YDHomeTableViewHeaderView


-(NSMutableArray *)buttonArray
{
    if (_buttonArray == nil) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
    
    static NSString *homeTableViewHeaderView = @"YDHomeTableViewHeaderView";
    YDHomeTableViewHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:homeTableViewHeaderView];
    if (headerView == nil) {
        headerView = [[YDHomeTableViewHeaderView alloc] initWithReuseIdentifier:homeTableViewHeaderView];
    }
    return headerView;
}


-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];

        [self lineView];
        
        self.headerViewH = isPhoneX ? rateX812(32) : rate750(32);
        [self setUpButtons];
        [self setUpTopButtonLine];
        
        SYNoHeightedButton *firstButton = self.buttonArray.firstObject;
        
        [self topButtonClick:firstButton];
        
    }
    return self;
}

-(UIView *)lineView
{
    if (_lineView == nil) {
        UIView *lineView = [[UIView alloc] init];
        [self.contentView addSubview:lineView];
        lineView.backgroundColor = NumberColor(247, 247, 247);
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.equalTo(@1);
        }];
        _lineView = lineView;
    }
    return _lineView;
}

//添加按钮
-(void)setUpButtons
{
    //标题文字
    NSArray *buttonTitles = @[@"交易动态",@"期权动态"];
    //按钮的尺寸
    CGFloat topButtonX = 0;
    CGFloat topButtonY = 0;
    CGFloat topButtonW = LKScreenW / buttonTitles.count;
    CGFloat topButtonH = _headerViewH;
    
    //创建按钮
    for (int i = 0; i < buttonTitles.count; i++) {
        
        SYNoHeightedButton *topButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        //绑定tag
        topButton.tag = i ;
        [topButton setTitle:buttonTitles[i] forState:UIControlStateNormal];
        topButtonX = i * topButtonW;
        topButton.frame = CGRectMake(topButtonX, topButtonY, topButtonW, topButtonH);
        
        [topButton addTarget:self action:@selector(topButtonClick:) forControlEvents:
         UIControlEventTouchUpInside];
        topButton.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        topButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [topButton setTitleColor:NumberColor(104, 114, 132) forState:UIControlStateNormal];
        [topButton setTitleColor:[UIColor colorWithHexString:Orange] forState:UIControlStateSelected];
        [self.contentView addSubview:topButton];
        
        [self.buttonArray addObject:topButton];
        
    }
}


-(void)setUpTopButtonLine
{
    //取出默认显示的第一个按钮
    SYNoHeightedButton *firstButton = self.buttonArray.firstObject;
    firstButton.selected = YES;
    self.previousButton = firstButton;
    
    UIView *lineView = [[UIView alloc] init];
    [self addSubview:lineView];
    CGFloat lineViewH = 1.0;
    CGFloat lineViewY = _headerViewH - lineViewH;
    lineView.backgroundColor = [UIColor colorWithHexString:Orange];
    lineView.centerX = firstButton.centerX;
    lineView.frame = CGRectMake(0, lineViewY, 0, lineViewH);
    
    lineView.width = firstButton.titleLabel.width;
    lineView.centerX = firstButton.centerX;
    self.lineView = lineView;
}



-(void)topButtonClick:(SYNoHeightedButton *)button
{
    self.previousButton.selected = NO;
    button.selected = YES;
    self.previousButton = button;
    
    //点击按钮的时候,tableview也会切换界面
    [UIView animateWithDuration:0.25 animations:^{
        //设置按钮下指示标的位置
        self.lineView.width = [button.currentTitle sizeWithAttributes:@{NSFontAttributeName : button.titleLabel.font}].width;
        self.lineView.centerX = button.centerX;
    } completion:^(BOOL finished) {
    }];
    
    
    if ([self.delegate respondsToSelector:@selector(homeTableViewHeaderView:didClickButtonTag:)]) {
        [self.delegate homeTableViewHeaderView:self didClickButtonTag:button.tag];
    }
}


@end
